package com.my.mvpdemo.mvp2.presenter;

import com.my.mvpdemo.Girl;
import com.my.mvpdemo.mvp2.model.GirlModelImp1;
import com.my.mvpdemo.mvp2.model.IGirlModel;
import com.my.mvpdemo.mvp2.view.IGirlView;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by 张英杰 on 2017/2/22.
 * 表示层 model和view 的桥梁
 */
public class GirlPresenter<T extends IGirlView> {

    //弱引用当 内存不足时销毁
    //View引用 销毁
//    IGirlView mGirlView;
    protected WeakReference<T> mViewRef; //软引用 只要gc活动那么就会 销毁
    //Model引用
    IGirlModel girlModel = new GirlModelImp1();

    public void attachView(T view) {
        this.mViewRef = new WeakReference<T>(view);
    }

    public void detachView() {
        if (mViewRef != null) {
            mViewRef.clear();
        }
    }


    //加载数据  n取来
    public void fetch() {
        //显示进度条
        if (mViewRef.get() != null) {
            mViewRef.get().showLoading();
        }
        if (girlModel != null) {
            //加载数据
            girlModel.loadGirl(new IGirlModel.GirlOnLoadListerer() {
                @Override
                public void onComplete(List<Girl> girls) {
                    //返回数据
                    //显示到view层
                    IGirlView girlView = mViewRef.get();
                    if (girlView != null) {
                        mViewRef.get().showGirls(girls);
                    }
                }
            });
        }
    }
}
